<?php
namespace App\Http\Controllers\Saas;

use App\Http\Config\ErrorCode;
use App\Models\Saas\Setting;
use App\Models\video\CommonServer;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class SettingController extends BaseController
{
    private $field = [
        "base" => ["logo" => null, "site_title" => null, "seo_title" => null, "copyright" => null],
        "sms" => ["sms_id" => null, "sms_secret" => null, "sms_sign" => null, "sms_code" => null],
        "tencent" => ["secret_id" => null, "secret_key" => null, "bucket" => null, "region" => null],
        "wxOpen" => ["open_app_id" => null, "open_app_secret" => null, "msg_verify_token" => null, "encoding_key" => null],
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = [
        "logo" => "logo", "site_title" => "站点标题", "seo_title" => "网站seo的标题", "copyright" => "版权信息", "sms_id" => "短信id",
        "sms_secret" => "短信secret", "sms_sign" => "短信签名", "sms_code" => "验证码模板ID", "secret_id" => "访问密钥ID",
        "secret_key" => "访问密钥key", "bucket" => "存储桶名", "region" => "地域", "open_app_id" => '第三方平台appId',
        "open_app_secret" => '第三方平台appSecret', "msg_verify_token" => '消息校验Token', "encoding_key" => '消息加解密Key'
    ];
    private $data = [];

    // 获取设置
    public function getSettingInfo(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(!$this->is_su){
            return $this->return_result(ErrorCode::code("customized", "您无权限操作"));
        }
        $settingData = Setting::getFields("*");
        if(!empty($settingData)){
            $settingData["logoUrl"] = $this->processingPictures($settingData["logo"]);
        }else{
            $settingData["logoUrl"] = "";
        }
        $data["base"]["logoUrl"] = $settingData["logoUrl"];
        foreach ($this->field as $k => $v){
            foreach ($v as $field => $i){
                $field = Str::camel($field);
                $data[$k][$field] = isset($settingData[$field]) ? $settingData[$field] : $i;
            }
        }
        $data["wxOpen"]["authEventUrl"] = CommonServer::getUrl('/videoAdmin/wxEvent/message/$APPID$');
        $data["wxOpen"]["msgEventUrl"] = CommonServer::getUrl("/videoAdmin/wxEvent/auth");
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 保存设置
    public function toSaveSettingInfo(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(!$this->is_su){
            return $this->return_result(ErrorCode::code("customized", "您无权限操作"));
        }
        if(empty($request["type"])){
            return $this->return_result(ErrorCode::code("params_not_exist", "type"));
        }
        if(!isset($this->field[$request["type"]])){
            return $this->return_result(ErrorCode::code("customized", "type参数错误"));
        }
        foreach ($this->field[$request["type"]] as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if ($v === 'require' && !isset($request[$ks])) {
                return $this->return_result(ErrorCode::code("params_not_exist", $this->fieldMsg[$k]));
            }
            if (isset($request[$ks])) {
                $this->data[$k] = trim($request[$ks]);
            }
        }
        $settingData = Setting::getFields("id");
        if(empty($settingData)){
            Setting::query()->insert($this->data);
        }else{
            Setting::query()->where(["id" => $settingData["id"]])->update($this->data);
        }
        $this->returnData["msg"] = "保存成功";
        return $this->return_result($this->returnData);
    }
}
